package com.sas.repositoryImpl;
 
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sas.domain.Article;
import com.sas.domain.Stock;
import com.sas.repository.StockRepository;

@Repository
public class HibernateStockRepository extends HibernateGenericRepository<Stock> 
		implements StockRepository {

	@Autowired
	public HibernateStockRepository (SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Stock> findByArticle(Article article, String property,
			boolean ascending) {
		Session session = getSession();
		String asc = ascending? " asc" : " desc";
		
		if (article == null) {
			return null;
		}
		
		Query query = session.createQuery("select s from Stock s inner join s.article a where a = ? " +
				"order by " + property + asc);
		query.setParameter(0, article);
		                                      	
		List<Stock> result = (List<Stock>)query.list();
		return result;
	}

}
